$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var machine = localStorage.getItem("gjxtwistwireMachine");
    var work_id = "";
    var work = "";
    var controlvalue_D = 2.800; //T
    var controlvalue_E = 0.500; //T
    var warehouseTou = false;
    var pass = 1;
    var focus = true;
    var qualityJson = {
        zj: "",
        bmzl: ""
    };

    $(".head .icon-lishi1").on("click", function () {
        var machine = $("section span.machine").html();
        if (machine != "空") {
            localStorage.setItem("gjxtwistwireMachine", machine);
        }
        window.location.href = "gjxtwistwire_lishi.html";
    });

    //搜索框 获取焦点
    $("#goods_id").focus();
    $("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });

    $("div.page").on("click", "span.personal", function () {
        localStorage.removeItem("gjxtwistwireMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });

    //根据 id 得到 工单信息、机台信息
    getMachineFun(machine);

    function getMachineFun(machine) {
        if (machine) {
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxtwistwire/getGjxtwistwirePlanAndMaterialByMachine',
                data: {
                    machine: machine
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if (data.gjxtwistwire && data.gjxtwistwire != "" && data.gjxtwistwire.status == "未接班") {
                            $.confirm("换班米长" + data.gjxtwistwire.length + "，是否确认？", function () {
                                $.ajax({
                                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                                    type: "post",
                                    url: url + '/app/gjxtwistwire/confirmChangeTeam',
                                    data: {
                                        machine: machine
                                    },
                                    dataType: 'json',
                                    json: 'callback',
                                    success: function (result) {
                                        if (result.success) {
                                            showplandata(res);
                                        } else {
                                            console.log(res);
                                            $alert(result.message);
                                        }
                                    },
                                    error: function (error) {
                                        console.log(error);
                                        $alert(error.responseText);
                                    }
                                });
                            })
                        } else {
                            //$alert(val + "扫描成功");
                            //填充 当前机台与当前用料
                            showplandata(res);
                        }
                    } else {
                        console.log(res)
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        } else {
            $("section span.machine").html("空");
        }
    }

    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描捻股机台！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/gjxtwistwire/getGjxtwistwirePlanAndMaterialByMachine',
                    data: {
                        machine: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        pass = 1;
                        loading_hide();
                        if (res.success) {
                            var data = res.result;
                            if (data.gjxtwistwire && data.gjxtwistwire != "" && data.gjxtwistwire.status == "未接班") {
                                $.confirm("换班米长" + data.gjxtwistwire.length + "，是否确认？", function () {
                                    $.ajax({
                                        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                                        type: "post",
                                        url: url + '/app/gjxtwistwire/confirmChangeTeam',
                                        data: {
                                            machine: val
                                        },
                                        dataType: 'json',
                                        json: 'callback',
                                        success: function (result) {
                                            if (result.success) {
                                                showplandata(res);
                                            } else {
                                                console.log(res);
                                                $alert(result.message);
                                            }
                                        },
                                        error: function (error) {
                                            console.log(error);
                                            $alert(error.responseText);
                                        }
                                    });
                                })
                            } else {
                                $alert(val + "扫描成功");
                                //填充 当前机台与当前用料
                                showplandata(res);
                            }
                        } else {
                            console.log(res);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });
    function showplandata(res) {
        var data = res.result;
        work = data.plan;
        if (res.controlvalue) {
            if (res.controlvalue["D"]) {
                controlvalue_D = res.controlvalue["D"];
            }
            if (res.controlvalue["E"]) {
                controlvalue_E = res.controlvalue["E"];
            }
        }
        $("section span.machine").html(data.machine.machineCode);
        $("section span.machineName").html(data.machine.machine);
        if (data.plan && data.plan.workplanNumber) {
            work_id = data.plan._id;
            $("section span.workplanNumber").html(data.plan.workplanNumber);
            $("section span.kh").html(data.plan.customerName);
            // $("section span.ropeGg").html(data.plan.ropeSpecification);
            $("section span.work_gg").html(data.plan.inventoryName);
            $("section span.work_mzmc").html(data.plan.detail.groupLength);
            $("section span.work_zs").html(data.plan.detail.groupNumber);
            $("section span.work_weight").html(data.plan.detail.planQuantity);
            $("section span.work_completeWheelCount").html(data.plan.detail.completeWheelCount ? data.plan.detail.completeWheelCount : "0");
            // $("section span.work_zkpts").html(data.plan.detail.zkpts);
            // $("section span.work_ywcts").html(data.plan.detail.ywcts);
            // $("section span.work_ywcls").html(data.plan.detail.ywcls);
            // $("section span.work_syls").html(data.plan.detail.syls);
        } else {
            $("section span.workplanNumber").html("空");
            $("section span.kh").html("空");
            // $("section span.ropeGg").html("空");
            $("section span.work_gg").html("空");
            $("section span.work_mzmc").html("空");
            $("section span.work_zs").html("空");
            $("section span.work_weight").html("空");
            $("section span.work_completeWheelCount").html("空");
            // $("section span.work_zkpts").html("空");
            // $("section span.work_ywcts").html("空");
            // $("section span.work_ywcls").html("空");
            // $("section span.work_syls").html("空");
        }
        if (data.material && data.material.length > 0) {
            $("section div.material span.number").html(data.material[0].batch);
            $("section div.material span.gggh").html((data.material[0].inventorySteel || "") + " " + (data.material[0].inventoryDiameter || ""));
            $("section div.material span.lh").html(data.material[0].stove);
            $("section div.material span.column").html(data.material[0].column);
            $("section div.material span.quantity").html(data.material[0].quantity || 0);
            setAllmaterial(data.material);
            // if (Number(data.material[0].quantity) < 50) {
            //     $alert("当前原料剩余重量小于50KG了，请检查", 3000);
            // }
        } else {
            $("section div.material span.number").html("空");
            $("section div.material span.gggh").html("空");
            $("section div.material span.lh").html("空");
            $("section div.material span.column").html("空");
            $("section div.material span.quantity").html("空");
            setAllmaterial([]);
        }
    }

    function setAllmaterial(material) {
        console.log(material.length)
        var allMaterialHtml = "";
        for (var i = 1; i < material.length; i++) {
            allMaterialHtml += '  <div class="card-content-inner">' +
                '<a href="#"><span class="icon iconfont icon-tui tl pull-right"></span></a>' +
                '<p><span>编号 ：</span><span class="number">' + material[i].batch + '</span></p>' +
                '<p><span>规格钢号 ：</span><span class="gggh">' + (material[i].inventorySteel || "") + " " + (material[i].inventoryDiameter || "") + '</span></p>' +
                // '<p><span>炉号 ：</span><span class="lh">' + material[i].stove + '</span>&nbsp;&nbsp;<span>卷号 ：</span><span class="column">' + material[i].column + '</span></p>' +
                '<p><span>剩余重量 ：</span><span class="quantity">' + (material[i].quantity || 0) + '</span></p></div>'
        }
        $(".allMaterial").empty().html(allMaterialHtml).hide();
    }

    // 点击 下箭头
    $(".material").on("click", ".aaa", function (event) {
        var classname = $(this).attr("class");
        if (classname.indexOf("icon-xiashuangjiantou") > -1) {
            $(".allMaterial").show();
            $(this).removeClass("icon-xiashuangjiantou");
            $(this).addClass("icon-shangshuangjiantou");

        } else{
            $(".allMaterial").hide();
            $(this).removeClass("icon-shangshuangjiantou");
            $(this).addClass("icon-xiashuangjiantou");
        }
    });

    //点击 上料
    $(".bnt .shangliao").on("click", function () {
        var machine = $("section span.machine").html();
        var workplanNumber = $("section span.workplanNumber").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描机台号");
        }
        if (!machine || machine == "空") {
            $alert("请先扫描捻股机台");
        } else {
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxtwistwire/isFeedMachine',
                dataType: 'json',
                data: {
                    id: work_id,
                    machine: $("section span.machine").html()
                },
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    if (res.success) {
                        var data = res.result;
                        loading_hide();
                        var quantity = $("section .material span.quantity").html();
                        var addMaterial = $("section .addMaterial .card-content").length;
                        //if (quantity != "空" && Number(quantity) > 300) return $alert("剩余重量过大，请核对");
                        //if (addMaterial && addMaterial > 0) return $alert("存在累加用料，不可上料");
                        localStorage.setItem("work_id", work_id);
                        localStorage.setItem("gjxtwistwireMachine", machine);
                        window.location.href = "gjxtwistwire_sl.html";
                    } else {
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    //隐藏提示符 显示
                    loading_hide();
                    if (error.status == 0) {
                        $alert("请求超时，请再试一次！");
                    } else {
                        console.log(error);
                        $alert(error.responseText);
                    }
                }
            });
        }
    });

    // 点击 清楚 原料
    $(".bnt .clear").on("click", function () {
        var machine = $("section span.machine").html();
        var workplanNumber = $("section span.workplanNumber").html();
        var quantity = $("section div.material span.quantity").html();
        var number = $("section div.material span.number").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描机台号");
        }
        if (!machine || machine == "空") {
            $alert("请先扫描捻股机台");
        } else {
            $.confirm("当前用料剩余" + quantity + "，确认清除吗？", function () {
                loading_show("清除中...");
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/gjxtwistwire/clearMachinematerial',
                    dataType: 'json',
                    data: {
                        id: work_id,
                        batch: number,
                        machine: machine
                    },
                    json: 'callback',
                    cache: false,
                    timeout: 60000,
                    success: function (res) {
                        if (res.success) {
                            var data = res.result;
                            loading_hide();
                            $alert("清除成功");
                            localStorage.setItem("gjxtwistwireMachine", machine);
                            setTimeout(function () {
                                window.location.href = "gjxtwistwire.html";
                            }, 1200);
                        } else {
                            console.log(res);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        //隐藏提示符 显示
                        loading_hide();

                        if (error.status == 0) {
                            $alert("请求超时，请再试一次！");
                        } else {
                            console.log(error);
                            $alert(error.responseText);
                        }
                    }
                });
            })
        }
    });

    //点击 收线
    $(".bnt .shouxiao_wei").click(function () {
        var workplanNumber = $("section span.workplanNumber").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描机台号");
        }
        if ($("div.material span.number").html() && $("div.material span.number").html() != "空") {
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxtwistwire/getDataByMachine',
                dataType: 'json',
                data: {
                    id: work_id,
                    type: "收线",
                    machine: $("section span.machine").html()
                },
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    if (res.success) {
                        var data = res.result;
                        loading_hide();
                        focus = false;
                        $(".mengceng").show();
                        $("div.div_shouxian").show();
                        $("div.div_shouxian input").val("");
                        $("div.div_shouxian input.index").val(data.index);
                        $("div.div_shouxian input.axleLength").val(work.detail.axleLength || 0);
                        $("div.div_shouxian input.length").val(data ? data.length : "").focus();
                        // $("div.div_shouxian input.addLength").focus();
                        $("div.div_shouxian select").val("合格").trigger("change");
                        $("div.div_shouxian select.isShort").val("否").trigger("change");
                    } else {
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    //隐藏提示符 显示
                    loading_hide();

                    if (error.status == 0) {
                        $alert("请求超时，请再试一次！");
                    } else {
                        console.log(error);
                        $alert(error.responseText);
                    }
                }
            });
        } else {
            $alert("请先上料！");
        }
    });
    //点击 收线 取消
    $("div.div_shouxian .sx_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_shouxian").hide();
        $("#goods_id").focus();
        focus = true;
    });

    //点击 收线 确定
    $(".sx_queding").click(function () {
        // var zj = $("section span.work_gg").text().split(",")[0].replace(/m/ig, "");
        // var zj_Min = Number(accSub(work.detail.inventoryDiameter, Math.abs(work.detail.diameterToleranceMin)));
        // var zj_Max = Number(accAdd(work.detail.inventoryDiameter, Math.abs(work.detail.diameterToleranceMax)));
        var qualityInspectFrequency = work.detail.qualityInspectFrequency;
        // console.log(zj_Max)
        // console.log(zj_Min)
        // console.log(qualityInspectFrequency)
        if (!$("div.div_shouxian input.index").val()) {
            pass = 1;
            $alert("流水号不可为空");
            $("div.div_shouxian input.index").focus();
        }
        else if ($("div.div_shouxian input.index").val() < 0) {
            pass = 1;
            $alert("流水号不可小于0");
            $("div.div_shouxian input.index").focus();
        }if (!$("div.div_shouxian input.actualDiameter").val()) {
            pass = 1;
            $alert("实测直径不可为空");
            $("div.div_shouxian input.actualDiameter").focus();
        }
        else if ($("div.div_shouxian input.actualDiameter").val() < 0) {
            pass = 1;
            $alert("实测直径不可小于0");
            $("div.div_shouxian input.actualDiameter").focus();
        }
        // else if (!$("div.div_shouxian input.addLength").val()) {
        //     pass = 1;
        //     $alert("加米长不可为空");
        //     $("div.div_shouxian input.addLength").focus();
        // }
        else if (Number($("div.div_shouxian input.length").val()) <= 0) {
            pass = 1;
            $alert("实际米长不可小于0");
            $("div.div_shouxian input.length").focus();
        }
        else if ($("div.div_shouxian select.selfResult").val() == "不合格" && !$("div.div_shouxian select.bhgx").val()) {
            pass = 1;
            $alert("请选择不合格项");
        }
        else {
            // if (Number($("div.div_shouxian input.actualDiameter").val()) > Number(zj_Max) || Number($("div.div_shouxian input.actualDiameter").val()) < Number(zj_Min)) {
            //     qualityJson.zj = "直径不合格";
            // } else {
            //     qualityJson.zj = "";
            // }
            // if ($("div.div_shouxian .bmzl").val() == "不合格") {
            //     qualityJson.bmzl = "表面质量不合格";
            // } else {
            //     qualityJson.bmzl = "";
            // }
            var bhgx = [];
            for (var k in qualityJson) {
                if (qualityJson[k]) {
                    bhgx.push(qualityJson[k]);
                }
            }
            if ($("div.div_shouxian select.selfResult").val() == "不合格") {
                bhgx.push($("div.div_shouxian select.bhgx").val());
            }
            if (qualityInspectFrequency == "全检") {
                if (pass == 0) {
                    return;
                }
                pass = 0;
                //调用 收线 接口
                loading_show("收线中...");
                //拉丝 收线
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/gjxtwistwire/gjxtwistwireTakeup',
                    data: {
                        id: work_id,
                        machine: $("section span.machine").html(),
                        index: $("div.div_shouxian input.index").val(),
                        length: $("div.div_shouxian input.length").val(),
                        actualDiameter: $("div.div_shouxian input.actualDiameter").val(),
                        isShort: $("div.div_shouxian select.isShort").val(),
                        breakpoint: $("div.div_shouxian input.breakpoint").val(),
                        selfResult: $("div.div_shouxian select.selfResult").val(),
                        bhgx: JSON.stringify(bhgx),
                        printAmount: 3
                    },
                    timeout: 60000,
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            pass = 1;
                            //console.log(data);
                            takeupsuccess(res.workover);
                            // 不打印 钢绞线捻股标签
                        } else {
                            pass = 1;
                            loading_hide();
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        if (error == 0) {
                            loading_hide();
                            $alert("请求超时，请重新操作！");
                        } else {
                            console.log(error);
                            loading_hide();
                            $alert(error.responseText);
                        }
                    }
                });
            } else if (qualityInspectFrequency == "免检") {
                if (pass == 0) {
                    return;
                }
                pass = 0;
                //调用 收线 接口
                loading_show("收线中...");
                //拉丝 收线
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/gjxtwistwire/gjxtwistwireTakeup',
                    data: {
                        id: work_id,
                        machine: $("section span.machine").html(),
                        index: $("div.div_shouxian input.index").val(),
                        length: $("div.div_shouxian input.length").val(),
                        actualDiameter: $("div.div_shouxian input.actualDiameter").val(),
                        isShort: $("div.div_shouxian select.isShort").val(),
                        breakpoint: $("div.div_shouxian input.breakpoint").val(),
                        selfResult: $("div.div_shouxian select.selfResult").val(),
                        bhgx: JSON.stringify(bhgx),
                        printAmount: 1
                    },
                    timeout: 60000,
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            pass = 1;
                            //console.log(data);
                            takeupsuccess(res.workover);
                            // 不打印 钢绞线捻股标签
                        } else {
                            pass = 1;
                            loading_hide();
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        if (error == 0) {
                            loading_hide();
                            $alert("请求超时，请重新操作！");
                        } else {
                            console.log(error);
                            loading_hide();
                            $alert(error.responseText);
                        }
                    }
                });
            } else {
                $.modal({
                    title:  '是否留样？',
                    buttons: [
                        {
                            text: '否',
                            onClick: function() {
                                if (pass == 0) {
                                    return;
                                }
                                pass = 0;
                                //调用 收线 接口
                                loading_show("收线中...");
                                //拉丝 收线
                                $.ajax({
                                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                                    type: "post",
                                    url: url + '/app/gjxtwistwire/gjxtwistwireTakeup',
                                    data: {
                                        id: work_id,
                                        machine: $("section span.machine").html(),
                                        index: $("div.div_shouxian input.index").val(),
                                        length: $("div.div_shouxian input.length").val(),
                                        actualDiameter: $("div.div_shouxian input.actualDiameter").val(),
                                        isShort: $("div.div_shouxian select.isShort").val(),
                                        breakpoint: $("div.div_shouxian input.breakpoint").val(),
                                        selfResult: $("div.div_shouxian select.selfResult").val(),
                                        bhgx: JSON.stringify(bhgx),
                                        printAmount: 1
                                    },
                                    timeout: 60000,
                                    dataType: 'json',
                                    json: 'callback',
                                    success: function (res) {
                                        if (res.success) {
                                            pass = 1;
                                            //console.log(data);
                                            takeupsuccess(res.workover);
                                        } else {
                                            pass = 1;
                                            loading_hide();
                                            $alert(res.message);
                                        }
                                    },
                                    error: function (error) {
                                        pass = 1;
                                        if (error == 0) {
                                            loading_hide();
                                            $alert("请求超时，请重新操作！");
                                        } else {
                                            console.log(error);
                                            loading_hide();
                                            $alert(error.responseText);
                                        }
                                    }
                                });
                            }
                        },{
                            text: '是',
                            onClick: function() {
                                if (pass == 0) {
                                    return;
                                }
                                pass = 0;
                                //调用 收线 接口
                                loading_show("收线中...");
                                //拉丝 收线
                                $.ajax({
                                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                                    type: "post",
                                    url: url + '/app/gjxtwistwire/gjxtwistwireTakeup',
                                    data: {
                                        id: work_id,
                                        machine: $("section span.machine").html(),
                                        index: $("div.div_shouxian input.index").val(),
                                        length: $("div.div_shouxian input.length").val(),
                                        actualDiameter: $("div.div_shouxian input.actualDiameter").val(),
                                        isShort: $("div.div_shouxian select.isShort").val(),
                                        breakpoint: $("div.div_shouxian input.breakpoint").val(),
                                        selfResult: $("div.div_shouxian select.selfResult").val(),
                                        bhgx: JSON.stringify(bhgx),
                                        printAmount: 2
                                    },
                                    timeout: 60000,
                                    dataType: 'json',
                                    json: 'callback',
                                    success: function (res) {
                                        if (res.success) {
                                            pass = 1;
                                            //console.log(data);
                                            takeupsuccess(res.workover);
                                        } else {
                                            pass = 1;
                                            loading_hide();
                                            $alert(res.message);
                                        }
                                    },
                                    error: function (error) {
                                        pass = 1;
                                        if (error == 0) {
                                            loading_hide();
                                            $alert("请求超时，请重新操作！");
                                        } else {
                                            console.log(error);
                                            loading_hide();
                                            $alert(error.responseText);
                                        }
                                    }
                                });
                            }
                        }
                    ]
                });
            }
        }
    });

    function takeupsuccess(workover) {
        if (workover) {
            loading_hide();
            $(".mengceng").hide();
            $("div.div_shouxian").hide();
            $.alert("已经达到计划产量，确认完工吗？", function () {
                //收线完成，完工本机台计划
                loading_show("完工中...");
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/api/scheduleplanThird/twist/workover',
                    data: {
                        machine: $("section span.machine").html()
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (result) {
                        if (result.success) {
                            loading_hide();
                            $(".mengceng").hide();
                            $("div.div_shouxian").hide();
                            $.alert('当前计划已完工', function () {
                                localStorage.setItem("gjxtwistwireMachine", $("section span.machine").html());
                                // localStorage.removeItem("gjxtwistwireMachine");
                                window.location.href = "gjxtwistwire.html";
                            });
                        } else {
                            loading_hide();
                            $alert(result.message);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                });
            });
        } else {
            $(".mengceng").hide();
            $("div.div_shouxian").hide();
            $alert("收线成功");
            setTimeout(function () {
                localStorage.setItem("gjxtwistwireMachine", $("section span.machine").html());
                // localStorage.removeItem("gjxtwistwireMachine");
                window.location.href = "gjxtwistwire.html";
            }, 1200);
        }
    }

    //点击 退料
    $(".content").on("click", ".tl", function () {
        var workplanNumber = $("section span.workplanNumber").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描机台号");
        }
        if ($(this).parent().parent().find("span.number").html() && $(this).parent().parent().find("span.number").html() != "空") {
            focus = false;
            $(".mengceng").show();
            $("div.div_tuiliao").show();
            // $("div.div_tuiliao input.syzl").val($(this).parent().parent().find("span.quantity").html()).focus();
            $("div.div_tuiliao input.symc").val("").focus();
            $("div.div_tuiliao input.batch").val($(this).parent().parent().find("span.number").html());
            // $("div.div_tuiliao input.wheelNumber").val("");
        } else {
            $alert("退料编号不正确！");
        }
    });
    $(".bnt .returnMaterial").click(function () {
        var workplanNumber = $("section span.workplanNumber").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描机台号");
        }
        if ($("div.material span.number").html() && $("div.material span.number").html() != "空") {
            focus = false;
            $(".mengceng").show();
            $("div.div_tuiliao").show();
            $("div.div_tuiliao input").val("").focus();
            $("div.div_tuiliao input.batch").val("").focus();
        } else {
            $alert("请先上料！");
        }
    });
    //点击 退料 取消
    $("div.div_tuiliao .tl_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_tuiliao").hide();
        $("#goods_id").focus();
        focus = true;
    });
    //点击 退料 确定
    $("div.div_tuiliao .tl_queding").click(function () {
        if (!$("div.div_tuiliao input.batch").val()) {
            $alert("退料编号不可为空！");
            $("div.div_tuiliao input.batch").focus();
        } else {
            if ($("div.div_tuiliao input.symc").val() == 0) {
                //调用 收线 接口
                loading_show("清料中...");
                //拉丝 收线
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/gjxtwistwire/clearMaterialByBatch',
                    data: {
                        id: work_id,
                        batch: $("div.div_tuiliao input.batch").val(),
                        machine: $("section span.machine").html(),
                        alength: $("div.div_tuiliao input.symc").val()
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            loading_hide();
                            $alert("操作成功");
                            var machine = $("section span.machine").html();
                            localStorage.setItem("gjxtwistwireMachine", machine);
                            setTimeout(function () {
                                window.location.href = "gjxtwistwire.html";
                            }, 1200);
                        } else {
                            console.log(res);
                            loading_hide();
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                });
            } else {
                //调用 收线 接口
                loading_show("退料中...");
                //拉丝 收线
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/gjxtwistwire/gjxtwistwireReturnMaterial',
                    data: {
                        id: work_id,
                        batch: $("div.div_tuiliao input.batch").val(),
                        machine: $("section span.machine").html(),
                        alength: $("div.div_tuiliao input.symc").val()
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            loading_hide();
                            $alert("退料成功");
                            var machine = $("section span.machine").html();
                            localStorage.setItem("gjxtwistwireMachine", machine);
                            setTimeout(function () {
                                window.location.href = "gjxtwistwire.html";
                            }, 1200);
                            if (res.result) {
                                // 打印 拉丝 收线 标签
                                takeupPrint("silkTakeup", res.result);
                            }
                        } else {
                            console.log(res);
                            loading_hide();
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    //点击 换班
    $(".bnt .changeTeam").click(function () {
        var workplanNumber = $("section span.workplanNumber").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描排产单");
        }
        if ($("div.material span.number").html() && $("div.material span.number").html() != "空") {
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxtwistwire/getDataByMachine',
                dataType: 'json',
                data: {
                    id: work_id,
                    type: "换班",
                    machine: $("section span.machine").html()
                },
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    if (res.success) {
                        loading_hide();
                        focus = false;
                        var data = res.result;
                        $(".mengceng").show();
                        $("div.div_huanban").show();
                        $("div.div_huanban input.ywcmc").val(data.length).focus();
                    } else {
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    //隐藏提示符 显示
                    loading_hide();

                    if (error.status == 0) {
                        $alert("请求超时，请再试一次！");
                    } else {
                        console.log(error);
                        $alert(error.responseText);
                    }
                }
            });
        } else {
            $alert("请先上料！");
        }
    });
    //点击 换班 取消
    $("div.div_huanban .hb_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_huanban").hide();
        $("#goods_id").focus();
        focus = true;
    });
    //点击 换班 确定
    $("div.div_huanban .hb_queding").click(function () {
        if ($("div.div_huanban input.ywcmc") <= 0) {
            $alert("已完成米长应大于零");
            $("div.div_huanban input.ywcmc").focus();
        } else {
            //调用 换班 接口
            loading_show("换班中...");
            //拉丝 收线
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/gjxtwistwire/gjxtwistwireChangeTeam',
                data: {
                    id: work_id,
                    machine: $("section span.machine").html(),
                    length: $("div.div_huanban input.ywcmc").val()
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        var data = res.result;
                        loading_hide();
                        $alert("换班成功");
                        setTimeout(function () {
                            localStorage.removeItem("gjxtwistwireMachine");
                            window.location.href = "gjxtwistwire.html";
                        }, 1200);
                    } else {
                        console.log(res);
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    console.log(error);
                    loading_hide();
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 查看计划
    $(".bnt .lookPlan").click(function () {
        var machine = $("section span.machine").html();
        var workplanNumber = $("section span.workplanNumber").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描机台号");
        }
        if (!machine || machine == "空") {
            $alert("请先扫描捻股机台");
        } else {
            localStorage.setItem("gjxtwistwireMachine", machine);
            window.location.href = "gjxtwistwire_ckjh.html";
        }
    });

    //点击 完工
    $(".bnt .workover").click(function () {
        var machine = $("section span.machine").html();
        var workplanNumber = $("section span.workplanNumber").html();
        if (!workplanNumber || workplanNumber == "空") {
            return $alert("请先扫描机台号");
        }
        if (!machine || machine == "空") {
            $alert("请先扫描捻股机台");
        } else {
            $.confirm("确认完工吗？", function () {
                //调用 换班 接口
                loading_show("完工中...");
                //拉丝 机台 完工
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/api/scheduleplanThird/twist/workover',
                    data: {
                        machine: machine
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            loading_hide();
                            $alert("完工成功");
                            localStorage.removeItem("gjxtwistwireMachine");
                            setTimeout(function () {
                                window.location.href = "gjxtwistwire.html";
                            }, 1200);
                        } else {
                            console.log(res);
                            loading_hide();
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                });
            });
        }
    });

});
